package graphic;

import Serializable.ParsedCmd;
import input.Input;
import java.util.logging.Level;
import java.util.logging.Logger;
import klient.Bridge;
import klient.Klient;
import objects.Text2dPrinter;
import openGL11.Engine;
import org.lwjgl.opengl.Display;

/**
 *  
 * @author Bartek & Marcin
 * 
 * @description Multi Menu
 */
public class MenuMulti {
    Engine e;
    public boolean ready=false;
    public MenuMulti(){this.e=Graphic.e;}
    private void getList(){
        Bridge.setOpPending(true);
        Bridge.setCmd(new ParsedCmd("GETPLIST"));
        Bridge.setNeedAny(true);
    }
    private void ready(){
        ready=true;
        Bridge.setCmd(new ParsedCmd("READY"));
        Bridge.setNeedAny(true);
    }
    private void notReady(){
        ready=false;
        Bridge.setCmd(new ParsedCmd("NREADY"));
        Bridge.setNeedAny(true);
    }
    private void disconnect(){
        Bridge.setOpPending(true);
        Bridge.setCmd(new ParsedCmd("DISCONNECT"));
        Bridge.setNeedAny(true);
        while(Bridge.opPending){
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(MenuMulti.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    public void loop(){
        e.start2D();
        e.setClearColor(new Color(0,0,0,1));
        
        Text2dPrinter wrBig = new Text2dPrinter();        
        wrBig.setFont("Alan Den.ttf",40);
        wrBig.setColor(org.newdawn.slick.Color.white);
        
        Text2dPrinter wr = new Text2dPrinter();        
        wr.setFont("Alan Den.ttf",20);
        wr.setColor(org.newdawn.slick.Color.white);
        
        Text2dPrinter wrB = new Text2dPrinter();        
        wrB.setFont("Alan Den.ttf",20);
        wrB.setColor(org.newdawn.slick.Color.blue);
        
                Text2dPrinter wrR = new Text2dPrinter();        
        wrR.setFont("Alan Den.ttf",20);
        wrR.setColor(org.newdawn.slick.Color.red);
        
        Text2dPrinter wrG = new Text2dPrinter();        
        wrG.setFont("Alan Den.ttf",20);
        wrG.setColor(org.newdawn.slick.Color.green);
        
        Input.init();
        getList();
        while(true){
            e.clear();
            Input.refresh();
            wrBig.write(e.getWidth()/2-150, 10, "MultiDefence 3D");
            
            for(int i=0;i<4;++i){
                if(Bridge.players[i]!=null){
                    if(Bridge.ready[i]){
                        wrG.write(10, 300+i*40, Bridge.players[i]);
                    }else{
                        wrR.write(10, 300+i*40, Bridge.players[i]);
                    }
                }
            }
            
            if(Input.x()>20 && Input.x()<150 && Input.y()>200&&Input.y()<230){
                wrB.write(10, 200, "Gotowy");
            }else{
                if(ready) wrG.write(10, 200, "Gotowy");
                else wrR.write(10, 200, "Gotowy");
            }
            
            if(Input.x()>20 && Input.x()<150 && Input.y()>240&&Input.y()<280){
                wrB.write(10, 240, "Wyjdz");
            }else{
                wr.write(10, 240, "Wyjdz");
            }
            
            
            
            if(Input.LMBdown() && Input.x()>20 && Input.x()<150){
                if(Input.y()>200&&Input.y()<230){
                    if(ready) notReady();
                    else ready();
                }else if(Input.y()>240&&Input.y()<280){
                    disconnect();
                    return;
                }
            }
            
            if(Bridge.startGame){
                /////////////////////////////////////////////////////////////
                //                 TU ZACZYNAMY GRĘ!!!                     //
                /////////////////////////////////////////////////////////////  
                Bridge.isMulti=true;
                Loop l=new Loop(Graphic.e);
            }
            
            Display.update();  
            Display.sync(60);
        }
        
    }
}
